# 梨视频爬取 https://www.pearvideo.com/
import requests

url = "https://www.pearvideo.com/video_1796983"
contId = url.split("_")[1]

videoStatusUrl = f"https://www.pearvideo.com/videoStatus.jsp?contId={contId}&mrd=0.06251572278253015"
# 重要:UA和防盗链
header = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Encoding": "gzip, deflate, br, zstd",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Connection": "keep-alive",
    "Cookie": "JSESSIONID=3FFB1B9A7DE1D9E62BFA18A36BCFF652; PEAR_UUID=92503e7a-4127-4b80-9c42-7c2a416f2335; _uab_collina=173165886398716584132217; tgw_l7_route=e0ec575606f95cc9ad003a83f9f8574f; p_h5_u=4B5D9E1F-748F-4784-8B83-E18111152C64",
    "Host": "www.pearvideo.com",
    # 溯源 定位当前请求的上一级
    "Referer": "https://www.pearvideo.com/video_1796983",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0",
    "X-Requested-With": "XMLHttpRequest",
    "sec-ch-ua": "\"Chromium\";v=\"130\", \"Microsoft Edge\";v=\"130\", \"Not?A_Brand\";v=\"99\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}

# 响应视频地址格式:https://video.pearvideo.com/mp4/short/20241031/1731658964988-16039625-hd.mp4
# 实际视频地址格式:https://video.pearvideo.com/mp4/short/20241031/cont-1796983-16039625-hd.mp4
res = requests.get(videoStatusUrl, headers=header)
srcUrl = res.json()['videoInfo']['videos']['srcUrl']
systemTime = res.json()['systemTime']
srcUrl = srcUrl.replace(systemTime, f"cont-{contId}")
print(srcUrl)

# 下载视频
with open("a.mp4", mode="wb") as f:
    f.write(requests.get(srcUrl).content)